//
//  MOAlertView.m
//  WCImApp
//
//  Created by WCImApp on 2018/12/12.
//  Copyright © 2018 WCImApp. All rights reserved.
//

#import "MOAlertView.h"
#import "UIButton+QAExtension.h"
#import <YYCategories/YYCategories.h>

@interface MOAlertView () <UITextFieldDelegate>

@property (nonatomic, assign) MOAlertType alertType;

//遮罩
@property (nonatomic, strong) UIView *maskView1;
//弹窗背景
@property (nonatomic, strong) UIView *bgView;
//内容
@property (nonatomic, strong) UITextView *contentLab;
//内容textview
@property (nonatomic, strong) UITextView *contentTextView;
//拒绝按钮
@property (nonatomic, strong) UIButton *refuseBtn;
//同意按钮
@property (nonatomic, strong) UIButton *agreeBtn;
//时间
@property (nonatomic, strong) UILabel *dateLab;
//确定按钮
@property (nonatomic, strong) UIButton *okBtn;
//关闭按钮
@property (nonatomic, strong) UIButton *closeBtn;

//内容输入框
@property (nonatomic, strong) UITextField *contentTf;
//字数提示
@property (nonatomic, strong) UILabel *alertLab;

@end

@implementation MOAlertView

#pragma mark - Init

- (instancetype)initWithAlertType:(MOAlertType)alertType {
    self = [super init];
    if (self) {
        self.tag = 88888;
        self.alertType = alertType;
        self.textAlignment = NSTextAlignmentCenter;
        if (self.alertType == MOAlertTypeGreetingsToFriends) {
            [self keyboardNotification];
        }
        [self setupView];
    }
    
    return self;
}

#pragma mark - Keyboard Notification

- (void)keyboardNotification {
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification {
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.maskView1).offset(-height);
        make.centerX.equalTo(self.maskView1);
        make.height.mas_equalTo(204);
        make.width.mas_equalTo(285);
    }];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.maskView1);
        make.height.mas_equalTo(204);
        make.width.mas_equalTo(285);
    }];
}

#pragma mark - Private Methods

- (void)setupView {
    _maskView1 = [[UIView alloc] init];
    _maskView1.backgroundColor = [UIColor colorWithLightColor:[[UIColor blackColor] colorWithAlphaComponent:0.4f] DarkColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
    [self addSubview:_maskView1];
    
    _bgView = [[UIView alloc] init];
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 12;
    _bgView.backgroundColor = [UIColor assist_AlertBG_Color];
    [_maskView1 addSubview:_bgView];

    [_maskView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.maskView1);
        make.height.mas_equalTo(125);
        make.width.mas_equalTo(285);
    }];
    
    switch (self.alertType) {
        case MOAlertTypeDefault:
        {
            [self createDetault];
        }
            break;
        case MOAlertTypeGroupBulletin:
        {
            [self createGroupBulletin];
        }
            break;
        case MOAlertTypeWaitingDevelop:
        {
            [self createWaitingDevelop];
        }
            break;
        case MOAlertTypeLoginOut:
        case MOAlertTypeRecharge:
        {
            [self createLoginOut];
        }
        break;
        case MOAlertTypeGreetingsToFriends:
        {
            [self createGreetingsToFriends];
        }
            break;
            
        default:
            break;
    }
}

/**
 *  默认样式（标题、内容、是否按钮）
 */
- (void)createDetault {

    [_bgView addSubview:self.titleLab];
    
    self.contentLab.textAlignment = self.textAlignment;
    [_bgView addSubview:self.contentLab];
    
//    _refuseBtn = [[UIButton alloc] init];
//    _refuseBtn.backgroundColor = [UIColor whiteColor];
//    _refuseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//    [_refuseBtn setTitleColor:kRGB(157, 164, 179) forState:UIControlStateNormal];
//    _refuseBtn.layer.masksToBounds = YES;
//    _refuseBtn.layer.cornerRadius = 35/2;
//    _refuseBtn.layer.borderWidth = 1;
//    _refuseBtn.layer.borderColor = kRGB(157, 164, 179).CGColor;
//    [_refuseBtn addTarget:self action:@selector(refuseAction:) forControlEvents:UIControlEventTouchUpInside];
//    [_bgView addSubview:_refuseBtn];
//
//    _agreeBtn = [[UIButton alloc] init];
//    _agreeBtn.backgroundColor = main_blue_color;
//    _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//    [_agreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    _agreeBtn.layer.masksToBounds = YES;
//    _agreeBtn.layer.cornerRadius = 35/2;
//    [_agreeBtn addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
//    [_bgView addSubview:_agreeBtn];
    
//    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.bgView).offset(24);
//        make.centerX.equalTo(self.bgView);
//        make.left.equalTo(self.bgView).offset(16);
//        make.right.equalTo(self.bgView).offset(-16);
//    }];
//
//    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.titleLab.mas_bottom).offset(8);
//        make.centerX.equalTo(self.bgView);
//        make.left.equalTo(self.bgView).offset(16);
//        make.right.equalTo(self.bgView).offset(-16);
//        make.bottom.equalTo(self.refuseBtn.mas_top);
//    }];
//
//    [_refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.bgView).offset(-15);
//        make.right.equalTo(self.bgView.mas_centerX).offset(-10);
//        make.height.mas_equalTo(35);
//        make.width.mas_equalTo(120);
//    }];
//
//    [_agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.height.width.equalTo(self.refuseBtn);
//        make.left.equalTo(self.bgView.mas_centerX).offset(10);
//    }];
    
    _refuseBtn = [[UIButton alloc] init];
    _refuseBtn.backgroundColor = [UIColor clearColor];
    _refuseBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [_refuseBtn setTitleColor:mp_main_text_color forState:UIControlStateNormal];
    [_refuseBtn addTarget:self action:@selector(refuseAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_refuseBtn];

    _agreeBtn = [[UIButton alloc] init];
    _agreeBtn.backgroundColor = [UIColor clearColor];
    _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [_agreeBtn setTitleColor:main_blue_color forState:UIControlStateNormal];
    [_agreeBtn addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_agreeBtn];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(24);
        make.centerX.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(-16);
    }];
    
    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(8);
        make.centerX.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(-16);
        make.bottom.equalTo(self.refuseBtn.mas_top);
    }];
    
    [_refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView).offset(0);
        make.left.offset(0);
        make.height.mas_equalTo(45);
        make.right.equalTo(self.bgView.mas_centerX).offset(0);
    }];
    
    [_agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.equalTo(self.refuseBtn);
        make.left.equalTo(self.bgView.mas_centerX).offset(0);
        make.right.offset(0);
    }];
    
    //横
    UIView *hLine = [[UIView alloc] init];
    hLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:hLine];
    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.right.offset(0);
        make.bottom.offset(-45);
    }];
    //竖
    UIView *sLine = [[UIView alloc] init];
    sLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:sLine];
    [sLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.5, 45));
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.bottom.offset(0);
    }];
}

/**
 *  群公告弹窗
 */
- (void)createGroupBulletin {
////    _titleLab = [[UILabel alloc] init];
////    _titleLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];//[UIFont systemFontOfSize:16];
////    _titleLab.textColor = kRGB(58, 58, 58);
////    _titleLab.textAlignment = NSTextAlignmentCenter;
////    _titleLab.numberOfLines = 0;
//    [_bgView addSubview:self.titleLab];
//
//    _dateLab = [[UILabel alloc] init];
//    _dateLab.font = [UIFont systemFontOfSize:12];
//    _dateLab.textColor = kRGB(112, 122, 142);
//    _dateLab.textAlignment = NSTextAlignmentCenter;
//    [_bgView addSubview:_dateLab];
//
//    UIView *centerLine = [[UIView alloc] init];
//    centerLine.backgroundColor = kRGB(236, 239, 243);
//    [_bgView addSubview:centerLine];
//
//    [_bgView addSubview:self.contentLab];
//
//    [_bgView addSubview:self.okBtn];
//
//    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.bgView).offset(15);
//        make.centerX.equalTo(self.bgView);
//        make.left.equalTo(self.bgView).offset(16);
//        make.right.equalTo(self.bgView).offset(-16);
//    }];
//
//    [_dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.width.equalTo(self.titleLab);
//        make.top.equalTo(self.titleLab.mas_bottom).offset(5);
//    }];
//
//    [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.bgView).offset(10);
//        make.right.equalTo(self.bgView).offset(-10);
//        make.top.equalTo(self.dateLab.mas_bottom).offset(10);
//        make.height.mas_equalTo(0.5);
//    }];
//
//    // 群公告行高固定
//    CGSize stringSize = [_contentLab.text sizeWithMaxWidth:285 - 32 andFont:_contentLab.font];
//    CGFloat stringH = stringSize.height > WCI_ScaleHeight(534) ? WCI_ScaleHeight(534) : stringSize.height;
//    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(centerLine);
//        make.top.equalTo(centerLine.mas_bottom).offset(10);
//        make.width.equalTo(self.titleLab);
//        make.height.mas_equalTo(stringH);
//    }];
//
////    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.bottom.equalTo(self.bgView).offset(-15);
////        make.centerX.equalTo(self.bgView);
////        make.height.mas_equalTo(35);
////        make.width.mas_equalTo(120);
////    }];
//
//    //横
//    UIView *hLine = [[UIView alloc] init];
//    hLine.backgroundColor = [UIColor assist_AlertLine_Color];
//    [_bgView addSubview:hLine];
//    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(0.5);
//        make.left.right.offset(0);
//        make.bottom.offset(-45);
//    }];
//
//    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.bgView);
//        make.bottom.equalTo(self.bgView).offset(0);
//        make.height.mas_equalTo(45);
//        make.left.offset(0);
//    }];
    [_bgView removeFromSuperview];
    _bgView = nil;
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = assist_WB_color;
    [_maskView1 addSubview:self.bgView];
    
    UILabel *title = [[UILabel alloc] init];
    title.textColor = mp_main_text_color;
    title.font = FONT(@"PingFangSC-Semibold", 24);
    [title setText:(@"群公告")];
    [_bgView addSubview:title];
    self.titleLab = title;
    
    UILabel *dateLab = [[UILabel alloc] init];
    dateLab.font = FONT(@"PingFangSC-Regular", 12);
    dateLab.textColor = assist_text_color;
    [_bgView addSubview:dateLab];
    self.dateLab = dateLab;
//        self.textView = [[UITextView alloc] init];
    self.contentLab.font = FONT(@"PingFangSC-Regular", 14);
    _contentLab.textColor = mp_main_text_color;
    [_bgView addSubview:self.contentLab];
//        [self.view addSubview:self.textView];
//        self.textView.editable = NO;
    
    self.okBtn = [UIButton getMoBtn:(@"我知道了") font:WCIFontMedium(16)];
    ViewRadius(_okBtn, 8);
    [_bgView addSubview:_okBtn];
    [_okBtn addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 群公告行高固定
    CGSize stringSize = [_contentLab.text sizeWithMaxWidth:SCREEN_WIDTH - 64 andFont:_contentLab.font];
    CGFloat stringH = stringSize.height > WCI_ScaleHeight(434) ? WCI_ScaleHeight(434) : stringSize.height;
    
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_lessThanOrEqualTo(SCREEN_HEIGHT*0.8);
        make.top.equalTo(title.mas_top).offset(-50).priorityMedium();
    }];
    
    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.bottom.offset(-(WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT + 20));
        make.height.mas_equalTo(50);
        make.left.offset(32);
        make.right.offset(-32);
    }];
    
    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(32);
        make.right.offset(-32);
        make.bottom.offset(-(WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT + 100));
        make.height.mas_equalTo(stringH);
    }];
    
    [dateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(32);
        make.bottom.equalTo(self.contentLab.mas_top).offset(-30);
    }];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(32);
        make.bottom.equalTo(dateLab.mas_top).offset(-12);
    }];
    
}

/**
 *  待开发（标题、确定按钮）
 */
- (void)createWaitingDevelop {

    [_bgView addSubview:self.titleLab];

    [_bgView addSubview:self.okBtn];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(25);
        make.centerX.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(-16);
    }];
    
//    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.bgView).offset(-15);
//        make.centerX.equalTo(self.bgView);
//        make.height.mas_equalTo(35);
//        make.width.mas_equalTo(120);
//    }];
    
    //横
    UIView *hLine = [[UIView alloc] init];
    hLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:hLine];
    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.right.offset(0);
        make.bottom.offset(-45);
    }];
    
    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView).offset(0);
        make.height.mas_equalTo(45);
        make.left.offset(0);
    }];
}

/**
 *  登录被顶（标题、内容、确定按钮）2
 */
- (void)createLoginOut {
    if (self.alertType == MOAlertTypeRecharge) {
        self.contentTextView.textAlignment = self.textAlignment;//NSTextAlignmentCenter;
        self.contentTextView.textColor = mp_main_text_color;
    }

    [_bgView addSubview:self.titleLab];

    [_bgView addSubview:self.contentTextView];
    
    [_bgView addSubview:self.okBtn];
    
    [_bgView addSubview:self.closeBtn];
    
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(8);
        make.right.equalTo(self.bgView.mas_right).offset(-8);
        make.width.height.mas_equalTo(@0);
    }];

    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(25);
        make.centerX.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(-16);
    }];
    
    [_contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.titleLab);
        make.top.equalTo(self.titleLab.mas_bottom).offset(10);
        make.width.equalTo(self.titleLab);
        make.bottom.equalTo(self.okBtn.mas_top).offset(-5);
    }];
    
//    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.bgView).offset(-15);
//        make.centerX.equalTo(self.bgView);
//        make.height.mas_equalTo(35);
//        make.width.mas_equalTo(120);
//    }];
    
    //横
    UIView *hLine = [[UIView alloc] init];
    hLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:hLine];
    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.right.offset(0);
        make.bottom.offset(-45);
    }];
    
    [_okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView).offset(0);
        make.height.mas_equalTo(45);
        make.left.offset(0);
    }];
    
}

- (void)createGreetingsToFriends {

    self.titleLab.text = (@"问候好友");
    [_bgView addSubview:self.titleLab];
    
    UIView *titleLine = [[UIView alloc] init];
    titleLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:titleLine];
    
    UILabel *explainLab = [[UILabel alloc] init];
    explainLab.font = [UIFont systemFontOfSize:14];
    explainLab.textColor = assist_text_color;
    explainLab.text = (@"问候好友，让好友更快认出你");
    [_bgView addSubview:explainLab];
    
    _contentTf = [[UITextField alloc] init];
    _contentTf.font = [UIFont systemFontOfSize:14];
    _contentTf.textColor = mp_main_text_color;
    _contentTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _contentTf.contentMode = UITextFieldViewModeAlways;
    _contentTf.layer.masksToBounds = YES;
    _contentTf.layer.cornerRadius = 20;
    _contentTf.layer.borderWidth = 1;
    _contentTf.layer.borderColor = [UIColor colorWithHexString:@"E7E7E7"].CGColor;
    _contentTf.delegate = self;
    
    UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 40)];
    placeholderView.backgroundColor = [UIColor clearColor];
    
    _contentTf.leftView = placeholderView;
    _contentTf.leftViewMode = UITextFieldViewModeAlways;
    
    [_bgView addSubview:_contentTf];
    
    _alertLab = [[UILabel alloc] init];
    _alertLab.hidden = YES;
    _alertLab.font = [UIFont systemFontOfSize:11];
    _alertLab.text = (@"仅限输入50个字");
    _alertLab.textColor = kRGB(255, 29, 60);
    [_bgView addSubview:_alertLab];
    
    UIView *btnLine = [[UIView alloc] init];
    btnLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:btnLine];
    
    UIView *centerLine = [[UIView alloc] init];
    centerLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [_bgView addSubview:centerLine];
    
    UIButton *cancelBtn = [[UIButton alloc] init];
    cancelBtn.backgroundColor = [UIColor clearColor];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelBtn setTitle:(@"取消") forState:UIControlStateNormal];
    [cancelBtn setTitleColor:mp_main_text_color forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:cancelBtn];
    
    UIButton *sendBtn = [[UIButton alloc] init];
    sendBtn.backgroundColor = [UIColor clearColor];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn setTitle:(@"发送") forState:UIControlStateNormal];
    [sendBtn setTitleColor:main_blue_color forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:sendBtn];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(self.bgView).offset(15);
        make.height.mas_equalTo(16);
    }];
    
    [titleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(-16);
        make.top.equalTo(self.titleLab.mas_bottom).offset(15);
        make.height.mas_equalTo(0.5);
    }];
    
    [explainLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleLine);
        make.top.equalTo(titleLine.mas_bottom).offset(10);
        make.height.mas_equalTo(14);
    }];
    
    [_contentTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(explainLab.mas_bottom).offset(10);
        make.left.width.equalTo(titleLine);
        make.height.mas_equalTo(40);
    }];
    
    [_alertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTf.mas_bottom).offset(10);
        make.left.equalTo(self.contentTf).offset(15);
        make.height.mas_equalTo(11);
    }];
    
    [btnLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.alertLab.mas_bottom).offset(10);
        make.height.mas_equalTo(0.5);
    }];
    
    [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnLine.mas_bottom);
        make.bottom.centerX.equalTo(self.bgView);
        make.width.mas_equalTo(0.5);
    }];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.bgView);
        make.top.equalTo(btnLine.mas_bottom);
        make.right.equalTo(centerLine.mas_left);
    }];
    
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(centerLine.mas_right);
        make.top.equalTo(btnLine.mas_bottom);
        make.right.bottom.equalTo(self.bgView);
    }];
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.frame;

    [window addSubview:self];
//    @synchronized(self) {
//        [self mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(window);
//        }];
//    }

}

- (void)hidden {
    if (self.callBackToClose) {
        self.callBackToClose();
    }
    [self removeFromSuperview];
}

- (void)updateContentWithModel:(MOAlertModel *)model {
    switch (self.alertType) {
        case MOAlertTypeDefault:
        {
            _titleLab.text = model.title;
            _contentLab.text = model.content;
            if ([QAStringTool isBlankString:model.refuseTitle]) {
                model.refuseTitle = (@"取消");
            }
            if ([QAStringTool isBlankString:model.agreeTitle]) {
                model.refuseTitle = (@"确定");
            }

            [_refuseBtn setTitle:model.refuseTitle forState:UIControlStateNormal];
            [_agreeBtn setTitle:model.agreeTitle forState:UIControlStateNormal];
            
            
            if ([QAStringTool isBlankString:model.content]) {
                CGSize stringSize = [_titleLab.text sizeWithMaxWidth:285 - 32 andFont:_titleLab.font];
                CGFloat stringH = stringSize.height;
                
                [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.center.equalTo(self.maskView1);
                    make.height.mas_equalTo(100 + stringH );
                    make.width.mas_equalTo(285);
                }];
                
                return;
            }
            
            if ([QAStringTool isBlankString:model.title]) {
                CGSize stringSize = [_contentLab.text sizeWithMaxWidth:285 - 32 andFont:_contentLab.font];
                CGFloat stringH = stringSize.height;
                
                [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.center.equalTo(self.maskView1);
                    make.height.mas_equalTo(100 + stringH );
                    make.width.mas_equalTo(285);
                }];
                
                return;
            }
            
            _titleLab.numberOfLines = 1;
            CGSize stringSize = [_contentLab.text sizeWithMaxWidth:285 - 32 andFont:_contentLab.font];
            CGFloat stringH = stringSize.height;
            
            if (stringH > WCI_ScaleHeight(300)) {
                stringH = WCI_ScaleHeight(300);
            }
            
            [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.maskView1);
                make.height.mas_equalTo(125 + stringH );
                make.width.mas_equalTo(285);
            }];
        }
            break;
        case MOAlertTypeGroupBulletin:
        {
            _titleLab.text = model.title;
            _dateLab.text = model.date;
            _contentLab.text = model.content;
            [_okBtn setTitle:model.okTitle forState:UIControlStateNormal];
            
            // 群公告高固定
            CGSize stringSize = [_contentLab.text sizeWithMaxWidth:SCREEN_WIDTH - 64 andFont:_contentLab.font];
            CGFloat stringH = stringSize.height > WCI_ScaleHeight(390) ? WCI_ScaleHeight(390) : stringSize.height + 30;
            [_contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(stringH);
            }];
            [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.offset(0);
//                make.height.mas_lessThanOrEqualTo(SCREEN_HEIGHT*0.8);
                make.top.equalTo(self.titleLab.mas_top).offset(-50).priorityMedium();
            }];
            WeakSelf(ws);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 左上和右上为圆角
                UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:weakSelf.bgView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(12, 12)];
                CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
                cornerRadiusLayer.frame = weakSelf.bgView.bounds;
                cornerRadiusLayer.path = cornerRadiusPath.CGPath;
                weakSelf.bgView.layer.mask = cornerRadiusLayer;
                
            });
            
//            [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.offset(32);
//                make.right.offset(-32);
//                make.bottom.offset(-(WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT + 70));
//                make.height.mas_equalTo(stringH);
//            }];
        }
            break;
        case MOAlertTypeWaitingDevelop:
        {
            _titleLab.text = model.title;
            [_okBtn setTitle:model.okTitle forState:UIControlStateNormal];
            
            CGSize stringSize = [_titleLab.text sizeWithMaxWidth:285 - 32 andFont:_titleLab.font];
            CGFloat stringH = stringSize.height;
            
            [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.maskView1);
                make.height.mas_equalTo(100 + stringH);
                make.width.mas_equalTo(285);
            }];
        }
            break;
        
        case MOAlertTypeLoginOut:
        case MOAlertTypeRecharge:
        {
            _titleLab.text = model.title;
            _contentLab.text = model.content;
            _contentLab.textAlignment = self.textAlignment;
            _contentTextView.text = model.content;
            _contentTextView.textAlignment = self.textAlignment;
            [_okBtn setTitle:model.okTitle forState:UIControlStateNormal];
            
            double btnHeight = 0;
            if ([QAStringTool isBlankString:model.okTitle]) {
                btnHeight = 40;
                self.okBtn.hidden = YES;
                
                [self.okBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(self.bgView).offset(-15);
                    make.centerX.equalTo(self.bgView);
                    make.height.mas_equalTo(@0);
                    make.width.mas_equalTo(@0);
                }];
            }
            if (model.isShowClose) {
                [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.bgView.mas_top).offset(8);
                    make.right.equalTo(self.bgView.mas_right).offset(-8);
                    make.width.height.mas_equalTo(@45);
                }];
            }
            
            CGSize stringSize = [model.content sizeWithMaxWidth:285 - 32 andFont:_contentTextView.font];
            CGFloat stringH = stringSize.height + 20;
            if (stringH > WCI_ScaleHeight(350)) {
                stringH = WCI_ScaleHeight(350);
            }

            [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.maskView1);
                make.height.mas_equalTo(120 + stringH - btnHeight);
                make.width.mas_equalTo(285);
            }];
        }
        break;
        case MOAlertTypeGreetingsToFriends:
        {
            _contentTf.text = model.defaultContent;
            [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.maskView1);
                make.height.mas_equalTo(204);
                make.width.mas_equalTo(285);
            }];
        }
            break;
            
            
        default:
            break;
    }
}

#pragma mark - Click Event

//拒绝
- (void)refuseAction:(UIButton *)sender {
    [self removeFromSuperview];
    if (self.callBackToRefuse) {
        self.callBackToRefuse();
    }
}

//同意
- (void)agreeAction:(UIButton *)sender {
    [self removeFromSuperview];
    if (self.callBackToAgree) {
        self.callBackToAgree();
    }
}

//确定
- (void)okAction:(UIButton *)sender {
    [self removeFromSuperview];
    if (self.callBackToOk) {
        self.callBackToOk();
    }
}

//取消
- (void)cancelAction:(UIButton *)sender {
    [self removeFromSuperview];
}

//发送
- (void)sendAction:(UIButton *)sender {
    [self removeFromSuperview];
    if (self.callBackToSend) {
        self.callBackToSend(self.contentTf.text);
    }
}

- (void)enableTapGes {
    WeakSelf(ws);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf removeFromSuperview];
    }];
    [self addGestureRecognizer:tap];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.length == 1 && string.length == 0) {
        self.alertLab.hidden = YES;
        return YES;
    } else {
        if (textField.text.length >= 50) {
            self.alertLab.hidden = NO;
            textField.text = [textField.text substringToIndex:50];
            return NO;
        } else {
            self.alertLab.hidden = YES;
            return YES;
        }
    }
}

#pragma mark - lazy load

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];//[UIFont systemFontOfSize:16];
        _titleLab.textColor = mp_main_text_color;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.numberOfLines = 0;
    }
    return _titleLab;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [UITextView new];
        _contentTextView.font = [UIFont systemFontOfSize:15.0];
        _contentTextView.editable = NO;
        _contentTextView.selectable = NO;
        _contentTextView.textColor = mp_main_text_color;
        _contentTextView.backgroundColor = [UIColor clearColor];
    }
    return _contentTextView;
}

- (UIButton *)okBtn {
    if (!_okBtn) {
        _okBtn = [[UIButton alloc] init];
        [_okBtn setTitleColor:main_blue_color forState:(UIControlStateNormal)];
//        _okBtn.layer.masksToBounds = YES;
//        _okBtn.layer.cornerRadius = MONomalBtnRadius;
//        _okBtn.backgroundColor = main_blue_color;
        _okBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_okBtn addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

- (UITextView *)contentLab {
    if (!_contentLab) {
        _contentLab = [[UITextView alloc] init];
        _contentLab.font = [UIFont systemFontOfSize:15.0];//[UIFont fontWithName:@"Helvetica-Bold" size:12];
        _contentLab.editable = NO;
        _contentLab.textColor = UIColor.assist_AlertSub_Color;
        _contentLab.backgroundColor = [UIColor clearColor];

//        _contentLab.numberOfLines = 0;
    }
    return _contentLab;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setImage:[UIImage imageNamed:@"pop_icon_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

@end
